using System;
using System.Collections.Generic;
using System.Linq;
using SqlSugarEx;
using SqlSugarEx.Config;
using SqlSugarEx.Utilities;

namespace SiemensAmd.Mmt.DevQt
{
	public class ToolDeviceConfigDal
	{
		private readonly SqlSugarClientEx _dbSugar;

		private DbAppConfig _dbConfig;

		public ToolDeviceConfigDal(DbAppConfig dbConfig)
		{
			this._dbConfig = dbConfig;
			this._dbSugar = DbUtility.GetSugar(dbConfig, isAutoCloseCon: true);
		}

		public List<ToolDeviceConfigEntity> QueryEntities()
		{
			return this._dbSugar.Queryable<ToolDeviceConfigEntity>().ToList();
		}

		public int AddEntitySingle(ToolDeviceConfigEntity entity)
		{
			return this._dbSugar.Insertable(entity).ExecuteCommand();
		}

		public int EditEntitySingle(ToolDeviceConfigEntity entity)
		{
			return this._dbSugar.Updateable(entity).ExecuteCommand();
		}

		public int DeleteSelect(List<long> pkBuff)
		{
			return this._dbSugar.Deleteable<ToolDeviceConfigEntity>().In(pkBuff).ExecuteCommand();
		}

		public void CreateMagConfig(string fmcNo, string deviceNo, Dictionary<int, int> dicMagCapacity)
		{
			List<ToolDeviceConfigEntity> list = (from x in this._dbSugar.Queryable<ToolDeviceConfigEntity>()
				where x.DeviceNo == deviceNo
				select x).ToList();
			IEnumerable<IGrouping<int, ToolDeviceConfigEntity>> enumerable = from x in list
				group x by x.ToolInMag;
			List<ToolDeviceConfigEntity> addList = new List<ToolDeviceConfigEntity>();
			List<ToolDeviceConfigEntity> list2 = new List<ToolDeviceConfigEntity>();
			Action<int, int> action = delegate(int magNo, int placeNo)
			{
				addList.Add(new ToolDeviceConfigEntity
				{
					FMCNo = fmcNo,
					DeviceNo = deviceNo,
					ToolInMag = magNo,
					ToolInPlace = placeNo,
					PlaceValid = true
				});
			};
			if (list.Count == 0)
			{
				foreach (int key2 in dicMagCapacity.Keys)
				{
					for (int j = 1; j <= dicMagCapacity[key2]; j++)
					{
						action(key2, j);
					}
				}
			}
			foreach (IGrouping<int, ToolDeviceConfigEntity> item in enumerable)
			{
				int key = item.Key;
				List<ToolDeviceConfigEntity> source = item.ToList();
				if (!dicMagCapacity.ContainsKey(key))
				{
					foreach (ToolDeviceConfigEntity item2 in item)
					{
						list2.Add(item2);
					}
					continue;
				}
				int num = Math.Max(enumerable.Count(), dicMagCapacity[key]);
				int i;
				for (i = 1; i <= num; i++)
				{
					List<ToolDeviceConfigEntity> list3 = (from x in source
						where x.ToolInPlace == i
						orderby x.Id descending
						select x).ToList();
					if (i > dicMagCapacity[key])
					{
						list2.AddRange(list3);
					}
					else if (list3.Count == 0)
					{
						action(key, i);
					}
					else if (list3.Count > 1)
					{
						for (int k = 1; k < list3.Count; k++)
						{
							list2.Add(list3[i]);
						}
					}
				}
			}
			if (addList.Count > 0 || list2.Count > 0)
			{
				SqlSugarClientEx sugar = DbUtility.GetSugar(this._dbConfig, isAutoCloseCon: true);
				try
				{
					sugar.BeginTran();
					sugar.Insertable(addList).ExecuteCommand();
					sugar.Deleteable(list2).ExecuteCommand();
					sugar.CommitTran();
				}
				catch (Exception)
				{
					sugar.RollbackTran();
				}
			}
		}

		public int Clear()
		{
			return this._dbSugar.Deleteable<ToolDeviceConfigEntity>().ExecuteCommand();
		}
	}
}
